package health.lm.com.home.activity;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.StringUtils;
import com.lm.sjy.titlebar.widget.CommonTitleBar;
import com.lxj.xpopup.XPopup;

import butterknife.BindView;
import health.lm.com.component_base.base.mvp.activity.XActivity;
import health.lm.com.component_base.helper.ImageLoaderHelper;
import health.lm.com.component_base.popup.share.SharePopUtil;
import health.lm.com.component_base.popup.share.ShareProductUtil;
import health.lm.com.component_base.util.TimerHelper;
import health.lm.com.data.App;
import health.lm.com.home.R;
import health.lm.com.home.R2;
import health.lm.com.home.entity.InformationEntity;
import health.lm.com.home.presenter.InformationPresenter;
import health.lm.com.home.widget.CompletedView;
import health.lm.com.router.ARouterConstant;

import static com.lm.sjy.titlebar.widget.CommonTitleBar.ACTION_LEFT_BUTTON;
import static com.lm.sjy.titlebar.widget.CommonTitleBar.ACTION_RIGHT_BUTTON;

/**
 * @author GQI
 * @description: 资讯详情页
 * @date :2019-07-25 14:48
 */
@Route(path = ARouterConstant.InformationActivity)
public class InformationActivity extends XActivity<InformationPresenter> {
    @BindView(R2.id.title_bar)
    CommonTitleBar titleBar;
    @BindView(R2.id.tv_title)
    TextView tvTitle;
    @BindView(R2.id.tv_author)
    TextView tvAuthor;
    @BindView(R2.id.tv_date)
    TextView tvDate;
    @BindView(R2.id.wb_detail)
    WebView wbDetail;
    @BindView(R2.id.tv_icon_count)
    TextView tvIconCount;
    @BindView(R2.id.tv_count_down)
    TextView tvCountDown;
    @BindView(R2.id.iv_icon)
    ImageView ivIcon;
    @Autowired
    public String info_id = "";
    @BindView(R2.id.cv_progress)
    CompletedView cvProgress;
    private int mTotalProgress = 60;
    private int mCurrentProgress = 0;
    private boolean stopThread =false;
    /**退出activity时关闭所有定时器，避免造成资源泄漏。*/
    private CountDownTimer mCountDownTimer;
    private TimerHelper timerHelper;

    @Override
    public int getLayoutId() {
        return R.layout.activity_information;
    }

    @Override
    public InformationPresenter newP() {
        return new InformationPresenter();
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        mImmersionBar.statusBarColor(R.color.white).statusBarDarkFont(true).init();
        titleBar.setListener((v, action, extra) -> {
            if (ACTION_LEFT_BUTTON == action) {
                finish();
            } else if (ACTION_RIGHT_BUTTON == action) {
                new XPopup.Builder(context).asCenterList("请选分享方式", new String[]{"图片分享", "文字分享"}, (position, text) -> {
                    if (position == 0) {
                        SharePopUtil.getInstance().show(context, titleBar, info_id);
                    }else if (position == 1) {
                        SharePopUtil.getInstance().shareUrl(context, titleBar,SharePopUtil.TYPE_INFORMATION, info_id);
                    }
                }).show();
            }
        });
        getP().getData(info_id);
        getP().views(info_id);
    }

    public void setData(InformationEntity entity) {
        if (entity == null) {
            return;
        }
        //标题
        tvTitle.setText(entity.getTitle());
        //作者
        tvAuthor.setText(entity.getAuth());
        //时间
        tvDate.setText(entity.getCreate_time());
        //剩余糖果数
        tvIconCount.setText(entity.getLeave_sugar());
        //详情介绍
        wbDetail.loadDataWithBaseURL(null, entity.getDetail(), "text/html", "UTF-8", null);
        ImageLoaderHelper.getInstance().load(mActivity, App.getModel().getSugar_url(),ivIcon);
        if (StringUtils.equals("1",entity.getCan_reward())){
            cvProgress.setVisibility(View.VISIBLE);
            new Thread(new ProgressRunable()).start();
        }else {
            cvProgress.setVisibility(View.GONE);
        }


        if(StringUtils.equals("0",entity.getEnd_time())){
            tvCountDown.setText("已结束");
        }else {
            timerHelper = new TimerHelper();
            mCountDownTimer = timerHelper.onTimer(entity.getEnd_time(), new TimerHelper.TimerHelpCall() {
                @Override
                public void started(String time) {
                    tvCountDown.setText(time);
                }

                @Override
                public void setCancel(CountDownTimer downTimer) {

                }

                @Override
                public void finish() {

                }
            });
        }
    }



    class ProgressRunable implements Runnable {
        @Override
        public void run() {
            while (!stopThread && mCurrentProgress < mTotalProgress) {
                mCurrentProgress += 1;
                cvProgress.setProgress(mCurrentProgress,mTotalProgress);
                try {
                    Thread.sleep(1000);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (mCurrentProgress>=mTotalProgress){
                getP().getSugar(wbDetail,info_id);
            }
        }
    }

    @Override
    protected void onDestroy() {
        stopThread = true;
        super.onDestroy();
        if (mCountDownTimer != null){
            mCountDownTimer.cancel();
        }
    }
}
